package cn.org.xwb.main.utils;

import jakarta.servlet.http.HttpServletRequest;

/**
 * 和 http 相关的工具类
 */
public class HttpUtil {

    private HttpUtil() {}

    /**
     * 获取客户端的主机名、IP、端口号；返回格式：hostname(IP:port)
     *
     * @param request 请求信息对象
     * @return hostname(IP:port)
     */
    public static String getClientHostIpPort(HttpServletRequest request) {
        StringBuilder clientHostIpPort = new StringBuilder();
        // 客户端主机名
        clientHostIpPort.append(request.getRemoteHost());
        clientHostIpPort.append("(");
        // 客户端 IP
        clientHostIpPort.append(request.getRemoteAddr());
        clientHostIpPort.append(":");
        // 客户端端口号
        clientHostIpPort.append(request.getRemotePort());
        clientHostIpPort.append(")");
        return clientHostIpPort.toString();
    }

}
